package com.rech.xbexpress3;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 取件成功界面，TODO:需对比QjResultActivity进行整合
 * Created by sunshine on 2020/8/13
 */
public class FinishPickUpActivity extends AppCompatActivity {
    private int jjcount = 100;
    private Timer countTimer;
    private TextView tv_grid_num;
    private Button btn_open;
    private Button btn_conf;
    private String jj_gridnum = "";
    private String data = "";
    private TextView djs ;
    private ImageView imageView29;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation","");
        if (orientation.equals("landscape")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        jj_gridnum = getIntent().getStringExtra("jj_gridnum");
        data = getIntent().getStringExtra("data");
        setContentView(R.layout.activity_finish_pickup);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        getSupportActionBar().hide();
        //tv_grid_num = findViewById(R.id.tv_grid_num);
        //tv_grid_num.setText(jj_gridnum);
        //tv_grid_num.setVisibility(View.GONE);
        djs = findViewById(R.id.tv_djs);
        imageView29 = findViewById(R.id.imageView29);

        TextView jj1_back = findViewById(R.id.tv_back);
        jj1_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Exit();
                finish();
            }
        });


        btn_conf = findViewById(R.id.scan_open_cab);
        btn_open = findViewById(R.id.open_conf);
        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new FetchOpenLocalGridTaskv2ForQJ(data).execute();
            }
        });
        btn_conf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                //setResult(2002, intent);
                Exit();
                finish();
            }
        });

        countTimer=new Timer();
        countTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                jjcount--;
                //Jj1Activity.this.djs.setText(String.valueOf(OtherUtil.jjcount));

                Message message = new Message();
                message.what=20000;
                EventBus.getDefault().post(message);
                if(jjcount < 1){
                    //OtherUtil.restartApplication(Jj1Activity.this);
                    Exit();
                    finish();
                }
            }
        },0,1000);

        TextView tv_version = findViewById(R.id.tv_version);
        TextView tv_waiter = findViewById(R.id.tv_waiter);
        try {
            tv_version.setText("v" + OtherUtil.getVersion(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phone = sharedPreferences.getString("CabinetType_phone","");
        tv_waiter.setText("客服电话:" + phone);
        String cnImage     =  sharedPreferences.getString("CabinetType_gzh","");
        new FetchUrlImageTask(cnImage,imageView29).execute();
    }

    private void Exit(){
        countTimer.cancel();

        Message message = new Message();
        message.what=2002;
        EventBus.getDefault().post(message);

        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message){
        Intent intent = new Intent();
        switch(message.what){
            case 20000:
                handleDJS();
                break;
        }
    }
    private void handleDJS(){
        djs.setText(String.valueOf(jjcount));
    }
}
